# from app.db import db
from app.db import BaseDocument
import mongoengine as db
import datetime


class Asset(db.EmbeddedDocument):
    """

    Args:
        db ([type]): [description]
    """

    name = db.StringField()
    target = db.StringField(default='')
    sourceIp = db.StringField(default='')
    domain = db.StringField()
    index = db.IntField()
    # protocol = db.StringField(choices=['https:', 'http:'], default='http:')
    # host = db.StringField(default='')
    # hostname = db.StringField(default='')
    # port = db.LongField(min_value=1, max_value=65535)
    # eTLD1 = db.StringField(default='')
    status = db.StringField(choices=[None, '失败', '成功'])
    detail = db.StringField(default='未导入')


class BatchImportAsset(BaseDocument):
    uid = db.ObjectIdField(required=True)
    operator = db.StringField(default='')
    current_index = db.IntField(default=-1)
    total = db.LongField(min_value=0)
    status = db.StringField(choices=['doing', 'done'], default='doing')
    type = db.StringField(choices=['local', 'cloud'],  default='local', required=True)
    assets = db.ListField(db.EmbeddedDocumentField(Asset))
    create_time = db.DateTimeField()
    last_update_time = db.DateTimeField()
    assetType = db.StringField(default='web', choices=['web', 'host'])

    meta = {
        'collection': 'batchimportasset',
        'strict': False
    }

    def save(self, *args, **kwargs):
        if not self.create_time:
            self.create_time = datetime.datetime.utcnow()
        self.last_update_time = datetime.datetime.utcnow()
        return super(BatchImportAsset, self).save(*args, **kwargs)

